class EventEmitter {
  constructor() {
    this.eventList = {
      // 'hasHouse': [kang, wrap],
      // 'hasCar': [cheng]
    }
  }

  on(eventName, cb) {
    if (!this.eventList[eventName]) {
      this.eventList[eventName] = []
    }

    this.eventList[eventName].push(cb)
  }
  emit(eventName) {
    if (this.eventList[eventName]) {
      const handlers = this.eventList[eventName].slice()   // 
      handlers.forEach((item) => {
        item()
      })
    }
  }
  off(eventName, cb) {
    const callbacks = this.eventList[eventName]
    const index = callbacks.indexOf(cb)
    if (index !== -1) {
      callbacks.splice(index, 1)
    }
  }
  once(eventName, cb) {
    const wrap = () => {
      cb()
      this.off(eventName, wrap)
    }
    this.on(eventName, wrap)
  }
}

let _event = new EventEmitter()

function zheng() {
    console.log('小橘买房')
}
function liu() {
    console.log('小橘买二house')
}
function wu() {
    console.log('小橘车')
}


_event.on('hasHouse', zheng)
_event.once('hasHouse', liu) 
// _event.off('hasHouse', liu)//取消订阅
_event.on('hasCar', wu)



_event.emit('hasHouse')//发布 =》zheng函数的调用
_event.emit('hasHouse')//发布 =》liu函数的调用